<?php

/**
 * 话题推荐管理 -- 推荐提交程序
 * 
 * @copyright caijian
 * @since 2012-03-20
 */




include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $config,$g_cgival, $g_pro, $g_show;
	
	//check refer  -  add by xinwei
	if(! security_check::check_refer())
	{
		throw new Exception("来路不明");
	}
	
	// id
	cgi::both($g_cgival["id"], "id", 0);
	cgi::both($g_cgival["ids"], "ids", '');
	
	// op
	cgi::both($g_cgival["op"], "op", "");
	
	cgi::both($g_cgival["pos"], "pos", 0);
}

function get_data()
{
	global $config,$g_cgival, $g_pro, $g_show;
	
	$ids = explode(',',$g_cgival["ids"]);
	$op = $g_cgival["op"];
	$topic_tj_pos = intval($g_cgival['pos']);
	if (! $topic_tj_pos) $topic_tj_pos = 1;

	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();
	$admin_id = admin::admin_get_login_uid();
	$admin_name = admin::admin_get_login_user_name();

	$admin_log = new admin_log();
	$db = new db_food_topic();
	$fuser = new mod_food_user();

	if ($op == 'up' || $op == 'down') {

		$id = intval($g_cgival['id']);
		$data = $db->admin_get_food_topic_by_id($id);
		$datanext = $db->admin_get_food_topic_next($data['topic_seq'],$op);

		if ( is_array($datanext) && count($datanext) >0) {
			$db->admin_update_food_topic_by_id($data['id'],array('topic_seq'=>$datanext['topic_seq']));
			$db->admin_update_food_topic_by_id($datanext['id'],array('topic_seq'=>$data['topic_seq']));
		}
	
		$admin_log->admin_insert_log_info($admin_id, $site_id, 9, "排序 话题id={$id}");

	} else if ( $op == 'tj' || $op == 'untj') {

		$total_tj_num = $config['food_topic_total_tj_num'][$topic_tj_pos];

		foreach ($ids  as $id) {

			if(! $id) continue;

			if ($op == 'tj') {  // 添加推荐

				$db->admin_update_food_topic_tuijian($id,$site_id,$topic_tj_pos);
				$admin_log->admin_insert_log_info($admin_id, $site_id, 9, "推荐 话题id={$id}");

			} else { // 删除推荐

				$db->admin_update_food_topic_by_id($id, array('status'=>0));
				$admin_log->admin_insert_log_info($admin_id, $site_id, 9, "取消推荐 话题id={$id}");

			}

		}

	} else {
		throw new Exception("参数错误！");
	}	
}

try
{

	admin_check_login();
// 	admin_check_user_priv("foodv2/main_nav.php");
	
	check_cgi_pro();
	get_data();
	
	$g_show["error"] = 0;
	$g_show["errmsg"] = "成功";	

}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_show;
	
	echo json_encode($g_show);
}

?>